package com.example.demo.jpa.service.impl;

import com.example.demo.jpa.dao.SolitaireMapper;
import com.example.demo.jpa.entity.Solitaire;
import com.example.demo.jpa.service.SolitaireService;
import com.example.demo.jpa.service.SolitaireStatisticsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with Software IntelliJ IDEA
 * <p>
 *
 * @author Administrator  2019/9/15 13:25
 * @version 1.0
 */
@Service
public class SolitaireServiceImpl implements SolitaireService {

    @Autowired
    private SolitaireMapper solitaireMapper;
    @Autowired
    private SolitaireStatisticsService solitaireStatisticsService;

    @Override
    public Solitaire selectById(Long id) {
        return solitaireMapper.findById(id).orElse(null);
    }

    @Override
    public int updateByPrimaryKeySelective(Solitaire record) {
        record.setUpdateDate(new Date());
        solitaireMapper.save(record);
        return 1;
    }

    @Override
    public List<Solitaire> selectByParams(Map<String, Object> params) {

        Specification<Solitaire> specification = new Specification<Solitaire>() {
            @Override
            public Predicate toPredicate(Root<Solitaire> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                // 用于暂时存放查询条件的集合
                List<Predicate> predicatesList = new ArrayList<>();
                // --------------------------------------------
                // 查询条件示例
                // equal 示例
                if (params.containsKey("userId")) {
                    Predicate predicate = cb.equal(root.get("userId"), params.get("userId"));
                    predicatesList.add(predicate);
                }
                String title;
                if (StringUtils.isNotEmpty((title = (String) params.get("title")))) {
                    Predicate predicate = cb.like(root.get("title"), '%' + title + '%');
                    predicatesList.add(predicate);
                }

//                //between示例
//                if (birthday != null) {
//                    Predicate birthdayPredicate = cb.between(root.get("birthday"), birthday, new Date());
//                    predicatesList.add(birthdayPredicate);
//                }
//
//                //关联表查询示例
//                if (!StringUtils.isEmpty(courseName)) {
//                    Join<Student,Teacher> joinTeacher = root.join("teachers",JoinType.LEFT);
//                    Predicate coursePredicate = cb.equal(joinTeacher.get("courseName"), courseName);
//                    predicatesList.add(coursePredicate);
//                }
//
//                //复杂条件组合示例
//                if (chineseScore!=0 && mathScore!=0 && englishScore!=0 && performancePoints!=0) {
//                    Join<Student,Examination> joinExam = root.join("exams",JoinType.LEFT);
//                    Predicate predicateExamChinese = cb.ge(joinExam.get("chineseScore"),chineseScore);
//                    Predicate predicateExamMath = cb.ge(joinExam.get("mathScore"),mathScore);
//                    Predicate predicateExamEnglish = cb.ge(joinExam.get("englishScore"),englishScore);
//                    Predicate predicateExamPerformance = cb.ge(joinExam.get("performancePoints"),performancePoints);
//                    //组合
//                    Predicate predicateExam = cb.or(predicateExamChinese,predicateExamEnglish,predicateExamMath);
//                    Predicate predicateExamAll = cb.and(predicateExamPerformance,predicateExam);
//                    predicatesList.add(predicateExamAll);
//                }
                // ExampleMatcher

                query.orderBy(cb.desc(root.get("userId")));
                Predicate[] predicates = new Predicate[predicatesList.size()];
                return cb.and(predicatesList.toArray(predicates));
            }
        };
        return solitaireMapper.findAll(specification);
    }
}
